<template>
  <div class="bgcImages">
    <!-- 多语言 -->
    <div class="theMianBox">
      <img src="../../assets/logingBg.989212c0.png" alt="" />
    </div>
    <el-form
      :model="LoginFrom"
      :rules="LoginFromRules"
      action=""
      class="theRainBox"
      ref="loginFormRef"
    >
      <div class="hello">
        <el-switch v-model="value2"> </el-switch>
        <!-- 案例 -->
        <div class="index"></div>
      </div>
      <!-- 图片部分 -->
      <div class="iconBox">
        <span class="iconSpan">黑马面面</span>
        <span class="iconSoan">用户登录</span>
      </div>
      <!-- input输入框 -->
      <el-form-item prop="username">
        <el-input
          v-model="LoginFrom.username"
          placeholder="username"
          class="inputBox"
          prefix-icon="el-icon-user-solid"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-s-goods"
          v-model="LoginFrom.password"
          placeholder="possword"
          class="inputBox"
          :type="Type"
          ref="input"
        >
        </el-input>
        <div class="iconBox_1 iconfont icon-jiemao" @click="jj"></div>
      </el-form-item>
      <!-- 表单按钮 -->
      <el-button
        size="medium"
        @click="login"
        type="primary"
        class="buttonBox"
        >{{ $t("message.hello") }}</el-button
      >
    </el-form>
  </div>
</template>

<script>
import { login } from "@/api/login";
import { sha256 } from "js-sha256";
export default {
  data() {
    var checkEmail = (rule, value, callback) => {
      const regEmail = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
      if (regEmail.test(value)) {
        return callback();
      }
      callback(new Error("Please enter a valid email address"));
    };
    return {
      LoginFrom: {
        username: "root@admin.com",
        password: "123456",
      },
      Type: "password",
      buttonBox: false,
      value2: false,
      LoginFromRules: {
        username: [
          {
            required: true,
            message: "Please enter the email user name",
            trigger: "blur",
          },
          { validator: checkEmail, trigger: "blur" },
        ],
        password: [
          {
            required: true,
            message: "The password can not be less than 6 digits",
            trigger: "blur",
          },
          {
            min: 6,
            max: 15,
            message: "The password can not be less than 6 digits",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        try {
          const form = { ...this.LoginFrom };
          form.password = sha256(form.password);
          const { data } = await login(form);
          console.log(data);
          this.$store.commit("getUser", data);
          this.$router.push("/home");
        } catch (err) {
          this.$message("登录失败，请稍后重试！");
        }
      });
    },

    jj() {
      console.log(this.$refs.input);
      this.Type = this.Type == "password" ? "text" : "password";
    },
  },
  watch: {
    value2: function(newValue, oldValue) {
      if (oldValue == true) {
        this.$i18n.locale = "cn";
      } else {
        this.$i18n.locale = "en";
      }
    },
  },
};
</script>

<style lang="less" scoped>
.theMianBox img {
  width: 100%;
  height: 100%;
}
.theRainBox {
  width: 420px;
  background-color: #f5f5f5;
  position: absolute;
  left: 20%;
  top: 20%;
  padding: 35px 20px 15px 35px;
}

.inputBox {
  width: 400px;
}
.iconBox {
  margin-bottom: 15px;
  .iconSpan {
    padding-left: 40px;
    font-size: 22px;
    line-height: 26px;
  }
  .iconSoan {
    padding-left: 10px;
    font-size: 22px;
    line-height: 26px;
  }
}
.buttonBox {
  width: 95%;
  margin-bottom: 30px;
  height: 45px;
  line-height: 20px;
  font-size: 22px;
}
/deep/.el-input__icon {
  height: 0 !important;
}
.hello {
  position: absolute;
  right: 36px;
  top: 37px;
}
.bgcKuang {
  width: 100%;
  height: 100%;
  position: relative;
}
.iconBox_1 {
  position: absolute;
  right: 18px;
  top: 2px;
  color: #ccc;
  width: 25px;
  height: 13px;
  cursor: pointer;
  font-size: 16px;
}
</style>
